package org.hghog.jlinkdemo.controller;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.text.Text;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.hghog.jlinkdemo.process.MainProcess;
import org.hghog.jlinkdemo.util.DefaultPath;
import org.hghog.jlinkdemo.util.OperatorInfo;

import java.io.File;

public class HelloController {

    protected final static OperatorInfo info = new OperatorInfo();

    private static boolean is_chinese = true;

    @FXML
    private TextField beginText;

    @FXML
    private TextField endText;

    @FXML
    private TextField inText;

    @FXML
    private TextField outText;

    @FXML
    private Text errorText;

    @FXML
    private Button language;

    @FXML
    private Label beginLabel;

    @FXML
    private Label endLabel;

    @FXML
    private Button inButton;

    @FXML
    private Button outButton;

    @FXML
    private Button beginButton;

    @FXML
    private Label typeLabel;

    @FXML
    private RadioButton offType;

    @FXML
    private RadioButton onType;

    @FXML
    private ChoiceBox<String> typeChoice;

    @FXML
    private Label splitLabel;

    @FXML
    private RadioButton offSplit;

    @FXML
    private RadioButton onSplit;

    @FXML
    private ChoiceBox<String> splitChoice;

    @FXML
    protected void onProcessButtonClick() {
        String begin = beginText.getText();
        String end = endText.getText();
        String inPath = inText.getText();
        String outPath = outText.getText();

        if (begin == null || end == null) {
            String text = is_chinese ? "请输入片头、片尾时间！！！" : "please enter start time and end time!!!";
            showError(text);
            return;
        }

        if (begin.trim().isEmpty() || end.trim().isEmpty()) {
            String text = is_chinese ? "请输入片头、片尾时间！！！" : "please enter start time and end time!!!";
            showError(text);
            return;
        }

        int beginTime;
        int endTime;
        try {
            beginTime = Integer.parseInt(begin);
            endTime = Integer.parseInt(end);
            info.setBegin(Math.max(beginTime, 0));
            info.setEnd(Math.max(endTime, 0));
        } catch (Exception e) {
            String text = is_chinese ? "片头、片尾请输入数字！！！" : "start time and end time must be a number!!!";
            showError(text);
            return;
        }

        if (inPath == null || inPath.isEmpty()) {
            String text = is_chinese ? "请选择视频目录！！！" : "please select the video folder!!!";
            showError(text);
            return;
        }

        File inDir = new File(inPath);
        if (!inDir.exists()) {
            String text = is_chinese ? "视频目录不存在！！！" : "the video folder does not exist!!!";
            showError(text);
            return;
        }

        if (outPath == null || outPath.isEmpty()) {
            String text = is_chinese ? "请选择保存目录！！！" : "please select the save folder!!!";
            showError(text);
            return;
        }

        File outDir = new File(outPath);
        if (!outDir.exists()) {
            String text = is_chinese ? "保存目录不存在！！！" : "the save folder does not exist!!!";
            showError(text);
            return;
        }

        // 重复执行时，防止保存文件夹有同名文件导致executor执行时阻塞主线程
        if (outDir.listFiles() != null && outDir.listFiles().length > 0) {
            String text = is_chinese ? "保存目录必须为空文件夹！！！" : "the save folder must be empty folder!!!";
            showError(text);
            return;
        }

        info.setInPath(inPath);
        info.setOutPath(outPath);

        // 保存默认路径
        DefaultPath.saveLastTimePath();

        // 开始跑脚本
        showError(null);
        MainProcess.start(info);
    }

    @FXML
    protected void chooseInFolder() {
        DirectoryChooser chooser = new DirectoryChooser();
        chooser.setInitialDirectory(new File(System.getProperty("user.home")));

        File directory = chooser.showDialog(new Stage());
        if (directory != null) {
            String path = directory.getAbsolutePath();
            inText.setText(path);
        }
    }

    @FXML
    protected void chooseOutFolder() {
        DirectoryChooser chooser = new DirectoryChooser();
        chooser.setInitialDirectory(new File(System.getProperty("user.home")));

        File directory = chooser.showDialog(new Stage());
        if (directory != null) {
            String path = directory.getAbsolutePath();
            outText.setText(path);
        }
    }

    @FXML
    protected void onSetLanguage() {
        if (is_chinese) {
            is_chinese = false;
            language.setText("中文");
            beginLabel.setText("start time");
            endLabel.setText("end time");
            inButton.setText("video dir");
            outButton.setText("save dir");
            beginText.setPromptText("please enter the start time in seconds");
            endText.setPromptText("please enter the ending time in seconds");
            inText.setPromptText("please select the video folder");
            outText.setPromptText("please select the save folder");
            beginButton.setText("BEGIN");
            offType.setText("off");
            onType.setText("on");
            typeLabel.setText("format");
            offSplit.setText("off");
            onSplit.setText("on");
            splitLabel.setText("split");
        } else {
            is_chinese = true;
            language.setText("ENGLISH");
            beginLabel.setText("片头秒数");
            endLabel.setText("片尾秒数");
            inButton.setText("视频目录");
            outButton.setText("保存目录");
            beginText.setPromptText("请输入剪掉片头时间，单位为秒（如: 90）");
            endText.setPromptText("请输入剪掉片尾时间，单位为秒（如: 90）");
            inText.setPromptText("请选择待剪视频文件夹");
            outText.setPromptText("请选择剪后视频文件夹");
            beginButton.setText("开始");
            offType.setText("禁用");
            onType.setText("启用");
            typeLabel.setText("转换格式");
            offSplit.setText("禁用");
            onSplit.setText("启用");
            splitLabel.setText("视频分片");
        }
    }

    @FXML
    protected void choiceType() {
        info.setType(typeChoice.getValue());
    }

    @FXML
    protected void enabledToType() {
        if (onType.isSelected()) {
            typeChoice.setDisable(false);
            info.setToType(true);
            info.setType(typeChoice.getValue());
        } else {
            typeChoice.setDisable(true);
            info.setToType(false);
        }
    }

    @FXML
    protected void choiceSplit() {
        info.setSplit(splitChoice.getValue());
    }

    @FXML
    protected void enabledToSplit() {
        if (onSplit.isSelected()) {
            splitChoice.setDisable(false);
            info.setToSplit(true);
            info.setSplit(splitChoice.getValue());
        } else {
            splitChoice.setDisable(true);
            info.setToSplit(false);
        }
    }

    private void showError(String error) {
        errorText.setText(error);
    }

}